<template>
  <el-dialog
    v-model="dialogVisible"
    title="日志"
    width="800px"
    destroy-on-close
    class="log-dialog"
    :close-on-click-modal="false"
  >
    <el-table :data="logData" border stripe style="width: 100%">
      <el-table-column type="index" label="#" width="60"></el-table-column>
      <el-table-column prop="operateTime" label="操作时间" width="180"></el-table-column>
      <el-table-column prop="operator" label="操作人" width="100"></el-table-column>
      <el-table-column prop="operateContent" label="操作内容"></el-table-column>
    </el-table>
    <div class="pagination-container" style="margin-top: 15px;">
      <el-pagination
        v-model:current-page="pagination.currentPage"
        v-model:page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <template #footer>
      <dialog-footer cancel-text="关闭" :confirm-text="null" @cancel="closeDialog" />
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, defineProps, defineEmits, watch } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  logData: {
    type: Array,
    default: () => []
  },
  total: {
    type: Number,
    default: 0
  },
  currentPage: {
    type: Number,
    default: 1
  },
  pageSize: {
    type: Number,
    default: 10
  }
})

const emit = defineEmits(['update:visible', 'size-change', 'current-change', 'refresh'])

// 内部响应式状态，保持与props同步
const dialogVisible = ref(props.visible)
const pagination = ref({
  currentPage: props.currentPage,
  pageSize: props.pageSize,
  total: props.total
})

// 监听props.visible的变化，同步到内部状态
watch(() => props.visible, (newValue) => {
  dialogVisible.value = newValue
})

// 监听内部状态dialogVisible的变化，通过事件通知父组件
watch(() => dialogVisible.value, (newValue) => {
  if (props.visible !== newValue) {
    emit('update:visible', newValue)
  }
})

// 监听分页props的变化
watch(() => props.currentPage, (newValue) => {
  pagination.value.currentPage = newValue
})

watch(() => props.pageSize, (newValue) => {
  pagination.value.pageSize = newValue
})

watch(() => props.total, (newValue) => {
  pagination.value.total = newValue
})

// 关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false
}

// 分页方法
const handleSizeChange = (size) => {
  pagination.value.pageSize = size
  emit('size-change', size)
}

const handleCurrentChange = (page) => {
  pagination.value.currentPage = page
  emit('current-change', page)
}
</script>

<style lang="scss" scoped>
.log-dialog {
  :deep(.el-dialog__header) {
    padding: 15px 20px;
    margin-right: 0;
    border-bottom: 1px solid #ebeef5;
  }
  
  :deep(.el-dialog__body) {
    padding: 20px;
  }
  
  :deep(.el-dialog__footer) {
    padding: 10px 20px;
    border-top: 1px solid #ebeef5;
  }
  
  .pagination-container {
    display: flex;
    justify-content: flex-end;
    margin-top: 15px;
  }
}
</style> 